package com.example.servicearea.dahua;

import com.example.servicearea.common.Res;
import com.example.servicearea.lib.NetSDKLib;
import com.example.servicearea.lib.ToolKits;
import com.example.servicearea.utils.PlateColorUtils;
import com.example.servicearea.utils.RemoveZero;
import com.example.servicearea.utils.VehicleTypeTransform;
import com.example.servicearea.utils.StringUtil;
import com.sun.jna.Pointer;

import javax.swing.*;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class VehiclePass implements NetSDKLib.fAnalyzerDataCallBack{
    private final TRAFFIC_INFO trafficInfo = new TRAFFIC_INFO();
    private static String dbUrl = "jdbc:mysql://localhost:3306/service?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8"; // 获取连接
    private static String dbUser = "root";
    private static String dbPassword= "Root@123456!";

    public Map<Long, PersonCapture> deviceInfoMap = new HashMap<>();

    public void setDeviceInfoMap(PersonCapture device) {
        deviceInfoMap.put(device.getStatHandle().longValue(), device);
    }

    private class TRAFFIC_INFO {
        private String m_EventName;              // 事件名称
        private String m_PlateNumber;          // 车牌号
        private String m_PlateType;               // 车牌类型
        private String m_PlateColor;              // 车牌颜色
        private String m_VehicleColor;              // 车身颜色
        private String m_VehicleType;          // 车身类型
        private String m_VehicleSize;              // 车辆大小
        private String m_FileCount;                  // 文件总数
        private String m_FileIndex;                  // 文件编号
        private String m_GroupID;                  // 组ID
        private String m_IllegalPlace;              // 违法地点
        private String m_LaneNumber;              // 通道号
        private NetSDKLib.NET_TIME_EX m_Utc;      // 事件时间
        private int m_bPicEnble;                  // 车牌对应信息，BOOL类型
        private int m_OffSet;                      // 车牌偏移量
        private int m_FileLength;                 // 文件大小
        private NetSDKLib.DH_RECT m_BoundingBox;  // 包围盒
    }

    public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType,
                      Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize,
                      Pointer dwUser, int nSequence, Pointer reserved) {
        PersonCapture device = deviceInfoMap.get(lAnalyzerHandle.longValue());
        System.out.println("有回调！");
        System.out.println("dwAlarmType: "+dwAlarmType);

        if (lAnalyzerHandle.longValue() == 0) {
            return -1;
        }


        if (dwAlarmType == NetSDKLib.EVENT_IVS_TRAFFICJUNCTION
                || dwAlarmType == NetSDKLib.EVENT_IVS_ALL) {
            // 获取识别对象 车身对象 事件发生时间 车道号等信息
            GetStuObject(dwAlarmType, pAlarmInfo, device);
        }
        return 0;
    }

    // 获取识别对象 车身对象 事件发生时间 车道号等信息
    private void GetStuObject(int dwAlarmType, Pointer pAlarmInfo, PersonCapture device) {
        if (pAlarmInfo == null) {
            return;
        }

        switch (dwAlarmType) {
            case NetSDKLib.EVENT_IVS_TRAFFICJUNCTION: ///< 交通卡口事件
            {
                NetSDKLib.DEV_EVENT_TRAFFICJUNCTION_INFO msg = new
                        NetSDKLib.DEV_EVENT_TRAFFICJUNCTION_INFO();
                ToolKits.GetPointerData(pAlarmInfo, msg);
                //System.out.println("原生信息为:" + msg);
                trafficInfo.m_EventName =
                        Res.string().getEventName(NetSDKLib.EVENT_IVS_TRAFFICJUNCTION);
                try {
                    trafficInfo.m_PlateNumber = new String(msg.stuObject.szText,
                            "UTF-8").trim();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
//                System.out.println("*********");
//                int length = 0;
//                for (int i = 0; i < msg.stuObject.szText.length; ++i) {
//                    System.out.println(msg.stuObject.szText[i]);
//                }
//                System.out.println("*********");
                trafficInfo.m_PlateType = new String(msg.stTrafficCar.szPlateType).trim();
                trafficInfo.m_LaneNumber = String.valueOf(msg.nLane);
                trafficInfo.m_PlateColor = new String(msg.stTrafficCar.szPlateColor).trim();
                trafficInfo.m_VehicleColor = new
                        String(msg.stTrafficCar.szVehicleColor).trim();
                trafficInfo.m_VehicleType = new
                        String(msg.stuVehicle.szObjectSubType).trim();
                trafficInfo.m_Utc = msg.UTC;

                String plateColor = RemoveZero.byteToStr(msg.stTrafficCar.szPlateColor);
                String vehicleType = RemoveZero.byteToStr(msg.stuVehicle.szObjectSubType);
                String plateNumber = PlateColorUtils.transformPlateColor(plateColor) + trafficInfo.m_PlateNumber;
                System.out.println("车牌号：" + plateNumber + "车牌length："+plateNumber.length());
                if (plateNumber == null || "".equals(plateNumber)){
                    plateNumber = "无车牌";
                }
                System.out.println("车牌颜色：" + RemoveZero.byteToStr(msg.stTrafficCar.szPlateColor));
                System.out.println("车型：" + RemoveZero.byteToStr(msg.stuVehicle.szObjectSubType));
                String time = StringUtil.clearStr(trafficInfo.m_Utc.toStringTime());
                Integer vehicleTypeNo = VehicleTypeTransform.getVehicleTypeNo(vehicleType);
                System.out.println("车辆类型代码：" + vehicleTypeNo);
                System.out.println("车流摄像机IP,ID:" + device.getDeviceIp() + "," + device.getDeviceId());

                device.saveVehicleData(plateNumber, time, vehicleTypeNo);

                break;
            }
            default:
                break;
        }
    }
}

